#pragma once
#ifndef WAV_H
#define WAV_H

#include <Arduino_AdvancedAnalog.h>
#include <DigitalOut.h>
#include <SD.h>
#include <SPI.h>

#define AUDIO_INPUT_PIN A6  // 麦克风输入引脚


// WAV文件头结构体
struct __attribute__((packed)) WAVHeader {
  // Chunk 1描述
  char chunkID[4];         // "RIFF"
  uint32_t chunkSize;      // 文件大小 - 8
  char format[4];          // "WAVE"

  // Sub-chunk 1描述
  char subchunk1ID[4];     // "fmt "
  uint32_t subchunk1Size;  // 16 for PCM
  uint16_t audioFormat;    // PCM = 1
  uint16_t numChannels;    // 1：单声道；2：立体声
  uint32_t sampleRate;     // 采样率
  uint32_t byteRate;       // sampleRate * numChannels * bitsPerSample/8
  uint16_t blockAlign;     // numChannels * bitsPerSample/8
  uint16_t bitsPerSample;  // 每个采样的比特数

  // Sub-chunk 2描述
  char subchunk2ID[4];     // "data"
  uint32_t subchunk2Size;  // 数据部分的大小
};

void initialWav();

// void recordAudio();

void recordAudio(const char *fileName, unsigned long time_record);

void beginTestAudio();

void calculateWavCharacteristic(const char *fileName);

// void playAudio();

void playAudio(const char *fileName);

short adcToVoltage(uint16_t adcValue, double referenceVoltage);

void finishRecording(const char *fileName);

// 写入WAV文件头
void writeWavHeader(File &file, uint32_t sampleRate, uint16_t numChannels, uint16_t bitsPerSample);

void printFileWavHeader(File &file);

uint32_t getFilePosition(File &file);

void printWavHeader(WAVHeader header);
void printWavHeaderFmt(WAVHeader header);
void printWavHeaderData(WAVHeader header);

#endif // WAV_H